package com.chagee.store.operation.server.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.chagee.personnel.api.event.bo.PersonnelInfo;
import com.personnel.auth.entity.InsertPersonnelAuth;
import com.personnel.auth.service.PersonnelAuthService;
import com.personnel.auth.util.PersonnelAuthutil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * 自动填充功能
 *
 * @author yang
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MyMetaObjectHandler implements MetaObjectHandler {

	private final PersonnelAuthService personnelAuthService;

	@Override
    public void insertFill(MetaObject metaObject) {
		PersonnelInfo user = null;
		try {
			user = personnelAuthService.getLoginUserInfoInWeb();
		} catch (Exception e) {
			log.error("personnelAuthService.getLoginUserInfoInServer throw exception. {}", e.getMessage());
		}
		this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
		this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
		if (Objects.nonNull(user)) {
			InsertPersonnelAuth personnelAuth = null;
			try {
				personnelAuth = PersonnelAuthutil.getInsertPersonnelAuth(user);
			} catch (Exception e) {
				log.error("PersonnelAuthutil.getInsertPersonnelAuth throw exception. {}", e.getMessage());
			}
			if (Objects.nonNull(personnelAuth)) {
				this.strictInsertFill(metaObject, "createUser", String.class,personnelAuth.getCreateUser());
				this.strictInsertFill(metaObject, "createDept", String.class,personnelAuth.getCreateDept());
			}
			this.strictInsertFill(metaObject, "updateFrom", String.class,user.getRealName());
			this.strictInsertFill(metaObject, "updateFromCode", String.class,user.getFeishuEmpNo());
		}
        //this.strictInsertFill(metaObject, "createBy", String.class, JwtUtils.tokenUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {

		PersonnelInfo user = null;
		try {
			user = personnelAuthService.getLoginUserInfoInWeb();
		} catch (Exception e) {
			log.error("personnelAuthService.getLoginUserInfoInServer().getId() throw exception [{}]. ", e.getMessage());
		}
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
		if (Objects.nonNull(user) && Objects.nonNull(user.getId())) {
			this.strictUpdateFill(metaObject, "updateUser", String.class,String.valueOf(user.getId()));
		}
       // this.strictUpdateFill(metaObject, "updateBy", String.class, JwtUtils.tokenUserId());
    }

	@Override
	public MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
		// 原方法，当填充字段不是null时，不会进行填充。即前端更新时携带了旧的update信息，就不会填充update
		// if (metaObject.getValue(fieldName) == null) {
		Object obj = fieldVal.get();
		if (Objects.nonNull(obj)) {
			metaObject.setValue(fieldName, obj);
		}
		// }

		return this;
	}

}
